package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.admin.bo.AdminBrandBo;
import com.cskaoyan.market.service.BrandCRUDService;
import com.cskaoyan.market.db.common.BasePageInfo;
import com.cskaoyan.market.db.common.CommonData;
import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.cskaoyan.market.util.ReflectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author TangHonghao
 * @since 2023/03/21 14:52
 */
public class BrandCRUDServiceImpl implements BrandCRUDService {
    @Override
    public CommonData<MarketBrand> queryBrand(BasePageInfo basePageInfo, String id, String name) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper brandMapper = session.getMapper(MarketBrandMapper.class);
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        try {
            if (!StringUtils.isEmpty(id)) {
                criteria.andIdEqualTo(Integer.valueOf(id.trim()));
                // criteria.andGoodsSnLike(goodsSn).andGoodsSnIsNotNull();
            }
            if (!StringUtils.isEmpty(name)) {
                criteria.andNameLike("%" + name.trim() + "%");
            }

        } catch (NumberFormatException e) {
            e.printStackTrace();
            return null;
        }
        marketBrandExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        List<MarketBrand> marketBrands = brandMapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrands);
        CommonData<MarketBrand> data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public List<MarketBrand> queryBrandById(Integer id) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper brandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.or().andIdEqualTo(id);
        List<MarketBrand> marketBrands = brandMapper.selectByExample(marketBrandExample);
        MarketBrand marketBrand = marketBrands.get(0);
        if (marketBrand.getDeleted()) {
            return null;
        }
        session.commit();
        session.close();
        return marketBrands;
    }

    @Override
    public List<MarketBrand> queryBrandByName(String name) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper brandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.or().andNameLike("%" + name + "%");
        List<MarketBrand> marketBrands = brandMapper.selectByExample(marketBrandExample);
        MarketBrand marketBrand = marketBrands.get(0);
        if (marketBrand.getDeleted()) {
            return null;
        }
        session.commit();
        session.close();
        return marketBrands;
    }

    @Override
    public void deleteBrandById(Integer id) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper brandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setId(id);
        marketBrand.setDeleted(true);
        marketBrand.setUpdateTime(LocalDateTime.now());
        brandMapper.updateByPrimaryKeySelective(marketBrand);
        session.commit();
        session.close();
    }

    @Override
    public MarketBrand createBrand(String name, String desc, BigDecimal floorprice) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper brandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setName(name);
        marketBrand.setDesc(desc);
        marketBrand.setFloorPrice(floorprice);
        marketBrand.setAddTime(LocalDateTime.now());
        marketBrand.setUpdateTime(LocalDateTime.now());
        marketBrand.setDeleted(false);
        // MarketBrandExample marketBrandExample = new MarketBrandExample();
        // brandMapper.updateByPrimaryKeySelective(marketBrand);
        brandMapper.insertSelective(marketBrand);
        session.commit();
        session.close();
        return marketBrand;
    }

    @Override
    public MarketBrand updateBrand(Integer id, String name, String desc, BigDecimal floorprice) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper brandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setName(name);
        marketBrand.setDesc(desc);
        marketBrand.setFloorPrice(floorprice);
        marketBrand.setUpdateTime(LocalDateTime.now());
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.or().andIdEqualTo(id);
        brandMapper.updateByExampleSelective(marketBrand, marketBrandExample);
        session.commit();
        session.close();
        return marketBrand;
    }

    @Override
    public MarketBrand update(AdminBrandBo adminBrandBo) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketBrandMapper brandMapper = sqlSession.getMapper(MarketBrandMapper.class);
        MarketBrand marketBrand = ReflectUtils.copyObject(adminBrandBo, MarketBrand.class);
        LocalDateTime now = LocalDateTime.now();
        marketBrand.setUpdateTime(now);
        // 先修改 , 在查找
        int row = brandMapper.updateByPrimaryKeySelective(marketBrand);

        // 修改后在查找一下-- 这行代码可以不要
        // MarketAd data = adMapper.selectByPrimaryKey(MmrketBrand.getId());
        sqlSession.commit();
        sqlSession.close();
        if (row < 1) {
            return null;
        }
        return marketBrand;
    }

}
